//
//  EditNoteViewController.m
//  FileLockerPro
//
//  Created by SDSL-3 on 7/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "EditNoteViewController.h"
#import "AssetManager.h"

@implementation EditNoteViewController
@synthesize create, name;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc {
    self.name = nil;
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[txtContents layer] setBorderColor:[[UIColor grayColor] CGColor]];
    [[txtContents layer] setBorderWidth:1.5];
    [[txtContents layer] setCornerRadius:8];
    
    if(self.create == NO){
        AssetManager *am = [[AssetManager alloc] init];
        NSString *contents = [am getNoteContents:name];
        [am release];
        
        txtTitle.text = name;
        txtContents.text = contents;
        
        txtTitle.enabled = NO;
        
    }
    else{
        btnDelete.hidden = YES;
    }
    
    //UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    //[self.view addGestureRecognizer:tgr];
    //[tgr release];
}

-(void)hideKeyboard{
    [txtTitle resignFirstResponder];
    [txtContents resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if([text isEqualToString:@"\n"] || [text isEqualToString:@"\r"]){
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)btnSave_Click:(id)sender{

    AssetManager *am = [[AssetManager alloc] init];
    
    [am saveNoteContents:txtContents.text WithName:txtTitle.text];
    
    if(self.create){
        
    }
    else{
        
    }
    
    [am release];
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)btnCancel_Click:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)btnDelete_Click:(id)sender{
    AssetManager *am = [[AssetManager alloc] init];
    [am deleteNote:self.name];
    [am release];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
